package com.wzt.car.controller;

import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.LoginContext;
import com.wzt.base.util.PageResult;
import com.wzt.car.CarDto;
import com.wzt.car.domain.Car;
import com.wzt.car.es.doc.CarDoc;
import com.wzt.car.query.CarQuery;
import com.wzt.car.query.CarSearchQuery;
import com.wzt.car.service.ICarSearchService;
import com.wzt.car.service.ICarService;
import com.wzt.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/car")
public class CarController {
    @Autowired
    public ICarService carService;
    @Autowired
    private ICarSearchService carSearchService;


    /**
     * 保存或修改
     *
     * @param car 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResponse addOrUpdate(@RequestBody Car car) {
        try {
            if (car.getId() != null) {
                carService.editOne(car);
            } else {
                carService.addOne(car);
            }
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResponse delete(@PathVariable("id") Long id) {
        try {
            carService.removeOne(id);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResponse delete(@RequestBody List<Long> ids) {
        try {
            carService.removeMul(ids);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 通过id获取
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResponse get(@PathVariable("id") Long id) {
        try {
            Car car = carService.getOne(id);
            return AjaxResponse.oneResponse().setData(car);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 获取所有
     *
     * @return
     */
    @GetMapping
    public AjaxResponse list() {

        try {
            List<Car> data = carService.getAll();
            return AjaxResponse.oneResponse().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageResult 分页对象
     */
    @PostMapping
    public AjaxResponse json(@RequestBody CarQuery query) {
        try {
            PageResult<Car> pageList = carService.getPage(query);
            return AjaxResponse.oneResponse().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/onSale")
    public AjaxResponse onSale(@RequestBody List<Long> ids) {
        try {
            Employee admin = LoginContext.getLoginAdmin();
            carService.onSale(new CarDto(admin.getId(), admin.getUsername(), ids));
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("上架失败！" + e.getMessage());
        }
    }

    @PostMapping("/offSale")
    public AjaxResponse offSale(@RequestBody List<Long> ids) {
        try {
            Employee admin = LoginContext.getLoginAdmin();
            carService.offSale(new CarDto(admin.getId(), admin.getUsername(), ids));
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("下架失败！" + e.getMessage());
        }
    }

    @PostMapping("/productList")
    public AjaxResponse productList(@RequestBody CarSearchQuery searchQuery) {
        try {
            PageResult<CarDoc> result = carSearchService.search(searchQuery);
            return AjaxResponse.oneResponse().setData(result);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("获取失败！" + e.getMessage());
        }
    }
}